#include <file_guard.h>

#include <cerrno>
#include <cstdio>
#include <cstring>
#include <fcntl.h>
#include <unistd.h>

FileGuard::FileGuard(const char *filename, int flag)
{
    fd = open(filename, flag);
    if (IsOK() == false) {
        printf("open: %s\n", strerror(errno));
    }
}

FileGuard::~FileGuard()
{
    if (IsOK()) {
        close(fd);
    }
}

bool FileGuard::IsOK() const
{
    return fd != -1;
}

int FileGuard::GetFd() const
{
    return fd;
}
